#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null

# if run with args...
case $1 in
 -gtkinstall)
 exec $APPDIR/functions -gtkinstall
 ;;
 -iconinstall)
 exec $APPDIR/functions -gtkinstall -icontheme
 ;;
 -preview)
 exec $APPDIR/functions -preview
 ;;
 -about)
 exec $APPDIR/functions -about
 ;;
 -help)
 exec defaulthtmlviewer $APPDIR/help.html
 ;;
esac

# make sure the gtkrc files are correct
echo "#--- This file is autowritten, do not edit ---#
# include the theme, icon, and personal rc files
include \"$HOME/.config/gtk/gtkrc.widgets\"" > $HOME/.gtkrc

echo "#--- This file is autowritten, do not edit ---#
# include the theme, icon, and personal rc files
include \"$HOME/.config/gtk-2.0/gtkrc.icons\"
include \"$HOME/.config/gtk-2.0/gtkrc.widgets\"
include \"$HOME/.config/gtk-2.0/gtkrc.fonts\"" > $HOME/.gtkrc-2.0


[ ! -d $HOME/.config/gtk-2.0 ] && mkdir $HOME/.config/gtk-2.0
[ ! -d $HOME/.config/gtk ] && mkdir $HOME/.config/gtk
WIDGETS=$HOME/.config/gtk-2.0/gtkrc.widgets
WIDGETS1=$HOME/.config/gtk/gtkrc.widgets
CURRENTWIDGETS="`cat $WIDGETS | grep gtkrc | rev | cut -f 3 -d '/' | rev`"
FONTS=$HOME/.config/gtk-2.0/gtkrc.fonts
CURRENTFONT="`cat $FONTS | grep gtk-font-name | cut -f 2 -d '"'`"
ICONS=$HOME/.config/gtk-2.0/gtkrc.icons
CURRENTICONS="`cat $ICONS | grep gtk-icon-theme | cut -f 2 -d '"'`"


# setup themes portion of gui
THEMES="<item>$CURRENTWIDGETS</item>"
THEMELOC="`ls -d /usr/share/themes/* $HOME/.themes/*`"
	for DIR in $THEMELOC
	do
	NAME="`basename $DIR`"
	[ -f $DIR/gtk-2.0/gtkrc ] && echo $NAME,$DIR/gtk-2.0/gtkrc >> \
	 $HOME/.config/themetmp
	done
	for THEME in `cat $HOME/.config/themetmp | grep -v ${CURRENTWIDGETS}, | cut -f 1 -d ','`
	do
	 THEMES="$THEMES
	 <item>$THEME</item>"
	done

# setup the icons portion of the gui
if [ ! "$CURRENTICONS" = "[none]" ];then
 ICONS="<item>$CURRENTICONS</item>"
else
 ICONS=""
fi

ICONDIRS="`ls -d /usr/share/icons/* $HOME/.icons/* | grep -v hicolor | grep -v jimmac | grep -v default | grep -v ROX | grep -v Tango-ROX | grep -v .svg | grep -v .png | grep -v .xpm`"
for DIR in $ICONDIRS
do
 basename $DIR >> $HOME/.config/icontmp
done
ICONTHEMES="`cat $HOME/.config/icontmp | tr "\n" " "`"
#rm -f $HOME/.config/icontmp
for THEME in $ICONTHEMES
do
 ICONS="$ICONS
 <item>$THEME</item>"
done


export MAIN_DIALOG="
<window title=\"$LOC_MAIN\" icon-name=\"gtk-preferences\">
 <vbox>
  <menubar>
   <menu>
    <menuitem icon=\"gtk-new\">
     <label>$LOC_GTKINSTALL</label>
     <action>EXIT:gtkinstall</action>
    </menuitem>
    <menuitem icon=\"gtk-new\">
     <label>$LOC_ICONINSTALL</label>
     <action>EXIT:iconinstall</action>
    </menuitem>
    <menuitem icon=\"gtk-find\">
     <label>$LOC_PREVIEW</label>
     <action>$APPDIR/functions -preview &</action>
    </menuitem>
    <separator></separator>
    <menuitem icon=\"gtk-quit\">
     <label>$LOC_QUIT</label>
     <action>EXIT:abort</action>
    </menuitem>
    <label>$LOC_FILE</label>
   </menu>
   <menu>
    <menuitem icon=\"gtk-about\">
     <label>$LOC_ABOUT</label>
     <action>$APPDIR/functions -about &</action>
    </menuitem>
    <separator></separator>
    <menuitem icon=\"gtk-help\">
     <label>$LOC_HELP</label>
     <action>defaulthtmlviewer $APPDIR/help.html &</action>
    </menuitem>
    <label>$LOC_HELP</label>
   </menu>
  </menubar>
  <tree>
   <label>$LOC_GTKTHEMES</label>
   $THEMES
   <variable>GTK_THEME</variable>
   <height>160</height><width>250</width>
   <action signal=\"cursor_changed\">$APPDIR/functions -widgets \$GTK_THEME</action>
  </tree>
  <tree>
   <label>$LOC_ICONTHEMES</label>
   $ICONS
   <item>[none]</item>
   <variable>ICON_THEME</variable>
   <height>100</height><width>250</width>
   <action signal=\"cursor_changed\">$APPDIR/functions -icons \$ICON_THEME</action>
  </tree>
  <frame $LOC_FONTS>
   <hbox>
    <entry editable=\"false\">
     <input>cat $HOME/.config/gtk-2.0/gtkrc.fonts | grep gtk-font-name | cut -f 2 -d '\"'</input>
     <variable>GTKFONT</variable>
    </entry>
    <button>
     <input file icon=\"preferences-desktop-font\"></input>
     <action>$APPDIR/functions -fonts</action>
     <action>refresh:GTKFONT</action>
    </button>
   </hbox>
  </frame>
  <hbox>
   <button>
    <label>$LOC_CLOSE</label>
    <input file stock=\"gtk-close\"></input>
   </button>
  </hbox>
 </vbox>
</window>"

gtkdialog3 --program MAIN_DIALOG > $HOME/.config/themedlg
. $HOME/.config/themedlg
[ -f $HOME/.config/themetmp ] && rm -f $HOME/.config/themetmp
[ -f $HOME/.config/icontmp ] && rm -f $HOME/.config/icontmp
rm -f $HOME/.config/themedlg

case $EXIT in
 abort)
 exit 0
 ;;
 gtkinstall)
 $APPDIR/functions -gtkinstall
 exec $0
 ;;
 iconinstall)
 $APPDIR/functions -gtkinstall -icontheme
 exec $0
 ;;
esac




